package net.lzzy.practices.web.course;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import net.lzzy.practices.constants.ApiConstants;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Result;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.services.ResultService;
import net.lzzy.practices.utils.AuthUtils;
import net.lzzy.practices.utils.StringUtils;
import net.lzzy.practices.web.models.ApiResult;
import net.lzzy.practices.web.models.KeyInfo;
import net.lzzy.practices.web.models.VmResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author qhh on 2022/6/8
 * Description:
 */

@Api(tags = "提交结果相关业务")
@CrossOrigin
@RestController
@RequestMapping(ApiConstants.ROUTE_RESULT_ROOT)
public class ResultApiController {

    private final ResultService service;


    @Autowired
    public ResultApiController(ResultService service) {
        this.service = service;
    }

    @ApiOperation(value = "某学生提交某一章节的所有题目",notes = "token(String)<br/>")
    @PostMapping(ApiConstants.ROUTE_RESULT_POST)
    public ApiResult postResult(@RequestBody JSONObject json) {
        String token = json.getString(ApiConstants.KEY_TOKEN);
        if (StringUtils.isEmpty(token)) {
            return new ApiResult(false, Messages.NO_PERMISSION.toString(), null);
        }
        JSONArray array = json.getJSONArray(ApiConstants.KEY_RESULT_OPTION_ARRAY);
        if (array == null) {
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        List<VmResult> results = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            JSONObject jo = array.getJSONObject(i);
            String strQuestionId = jo.getString(ApiConstants.KEY_RESULT_OPTION_QUESTION_ID);
            JSONArray arrOptions = jo.getJSONArray(ApiConstants.KEY_RESULT_OPTION_ID);
            UUID questionId = UUID.fromString(strQuestionId);
            List<UUID> optionIds = new ArrayList<>();
            arrOptions.forEach(o -> optionIds.add(UUID.fromString(o.toString())));
            results.add(new VmResult(service.add(questionId, optionIds, token)));
        }
        return new ApiResult(true, results.size() + "", results);

    }

    @ApiOperation(value = "删除某一提交的结果",notes = "1.token(String)<br/>2.strResultId(String)<br/>")
    @DeleteMapping(ApiConstants.ROUTE_RESULT_DEL)
    public ApiResult remove(@RequestBody KeyInfo info) {
        String token = info.getToken();
        String strResultId = info.getId();
        if (StringUtils.isEmpty(token) || StringUtils.isEmpty(strResultId)) {
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        UUID resultId = UUID.fromString(strResultId);
        Messages msg = service.remove(resultId, token);
        return new ApiResult(msg == Messages.SUCCESS, msg.toString(), null);
    }

    @ApiOperation(value = "获取某学生某个章节的所有题目结果" ,notes = "1.id(UUID)<br/>2.id(UUID)<br/>3.token(String)")
    @GetMapping(ApiConstants.ROUTE_RESULT_CHAPTER)
    public ApiResult getChapterResult(@RequestParam(name = "sid") UUID studentId,
                                      @RequestParam(name = "cid") UUID chapterId,
                                      @RequestParam String token) {
        if (AuthUtils.illegalUser(token, new int[]{User.TYPE_STUDENT})) {
            return new ApiResult(false, Messages.NO_PERMISSION.toString(), null);
        }
        List<VmResult> results = new ArrayList<>();
        service.getChapterResults(studentId, chapterId).forEach(result -> results.add(new VmResult(result)));
        return new ApiResult(true, results.size() + "", results);
    }
    @ApiOperation(value = "教师获取某个题目所有学生提交的结果" ,notes = "1.id(UUID)<br/>2.id(UUID)<br/>3.token(String)")
    @GetMapping(ApiConstants.ROUTE_RESULT_QUESTION)
    public ApiResult getQuestionResults(@RequestParam(name = "qid") UUID questionId,
                                        @RequestParam String token) {
        List<VmResult> results = new ArrayList<>();
        List<Result> list = service.getQuestionResults(questionId, token);
        if (list == null) {
            return new ApiResult(false, Messages.WRONG_ID.toString(), null);
        }
        list.forEach(result -> results.add(new VmResult(result)));
        return new ApiResult(true, results.size() + "", results);
    }

    @ApiOperation(value = "判断某章节某学生是否已提交答案" ,notes = "1.id(UUID)<br/>2.id(UUID)<br/>3.token(String)")
    @GetMapping(ApiConstants.ROUTE_RESULT_COMMITTED)
    public ApiResult isChapterCommitted(@RequestParam(name = "sid") UUID studentId,
                                        @RequestParam(name = "cid") UUID chapterId,
                                        @RequestParam String token) {
        Boolean result = service.isChapterCommitted(studentId, chapterId, token);
        String message = result == null ? Messages.NO_PERMISSION.toString() : "";
        return new ApiResult(true, message, result);
    }
}
